<?php
/**
 * 后台登录控制器
 */
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller
{
    /**
     * 后台登录
     */
    public function index()
    {
        if(IS_POST){
            $msg = array('errno'=>0, 'error'=>'', 'url'=>'');
            $username = I('username');
            $pwd = I('password', '', 'md5');
            $keepalive=I('keepalive');

            $user = M('sysuser')->where(array('username'=>$username))->find();

            if (!$user || $user['userpwd'] != $pwd){
                $msg['errno'] = 1;
                $msg['error'] = '用户名或密码错误';
                $this->ajaxReturn($msg);
            }

            if ($user['lock']){
                $msg['errno'] = 1;
                $msg['error'] = '用户被锁定';
                $this->ajaxReturn($msg);
            }

            $data = array(
                'userid' => $user['userid'],
                'lastlogintime' => time()
            );
            M('sysuser')->save($data);

            session('sys_user_info', $user);//将用户数据存入session

            if($keepalive){
                cookie('username',$username,3600000);//记住用户名
            }else{
                cookie('username',null);
            }

            $msg['error'] = '登录成功';
            $msg['url'] = U('Admin/Wechat/index');
            $this->ajaxReturn($msg);
        }else{
            $this->display();
        }
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        session('sys_user_info',null); // 删除session
        $this->redirect(U('Admin/Login/index'));
    }


    /*
     * 重置密码
     */
    public function reset_pwd()
    {
        if(IS_POST)
        {
            $old_pwd = I('post.old_pwd');
            $new_pwd = I('post.new_pwd');
            $re_pwd = I('post.re_pwd');

            $userInfo  = session('sys_user_info');
            $check_pwd = M('sysuser')->where(array('userid'=>$userInfo['userid'],'userpwd'=>md5($old_pwd)))->find();
            if(!$check_pwd) {
                $msg['errno'] = 1;
                $msg['error'] = '旧密码输入错误';
                $this->ajaxReturn($msg);
            }
            if($new_pwd != $re_pwd) {
                $msg['errno'] = 1;
                $msg['error'] = '两次输入的密码不一致';
                $this->ajaxReturn($msg);
            }
            //验证密码长度
            if(mb_strlen($new_pwd)<6 || mb_strlen($new_pwd)>16) {
                $msg['errno'] = 1;
                $msg['error'] = '密码长度长度不符合规则';
                $this->ajaxReturn($msg);
            }
            $update = M('sysuser')->where(array('userid'=>$userInfo['userid']))->save(array('userpwd'=>md5($new_pwd)));
            if($update) {
                $msg['errno'] = 0;
                $msg['error'] = '修改成功';
                $msg['url'] = U('Admin/Login/reset_pwd');
                $user = M('sysuser')->where(array('userid'=>$userInfo['userid']))->find();
                session('sys_user_info', $user);
                $this->ajaxReturn($msg);
            }else{
                $msg['errno'] = 1;
                $msg['error'] = '网络错误，请稍后再试！';
                $this->ajaxReturn($msg);
            }
        }
        $this->display();
    }
}